package com.zhj.util.state.switchs;

import com.zhj.util.state.StateHandleException;
import com.zhj.util.state.enems.NumSourceEventEnum;
import com.zhj.util.state.enems.NumSourceStateEnum;

/**
 * 号源状态管理
 * @author zhj
 * @create 2024-04-17-17:29
 */
public class NumSourceStateMachine {

    private NumSourceStateEnum currentState;

    public NumSourceStateMachine() {
        this.currentState = NumSourceStateEnum.RELEASE;
    }

    public NumSourceStateEnum getCurrentState() {
        System.out.println("current state is : " + currentState);
        return currentState;
    }

    public void handleEvent(NumSourceEventEnum event) throws Exception {
        switch (currentState) {
            case LOCKING:
                if (event == NumSourceEventEnum.RELEASE) {
                    currentState = NumSourceStateEnum.RELEASE;
                } else if (event == NumSourceEventEnum.CASH) {
                    currentState = NumSourceStateEnum.CASH;
                } else if (event == NumSourceEventEnum.REG) {
                    currentState = NumSourceStateEnum.REG_SUCCESS;
                } else if (event == NumSourceEventEnum.STOP) {
                    currentState = NumSourceStateEnum.STOP;
                } else {
                    throw new StateHandleException(currentState, event);
                }
                break;
            case RELEASE:
                if (event == NumSourceEventEnum.LOCK) {
                    currentState = NumSourceStateEnum.LOCKING;
                } else if (event == NumSourceEventEnum.PRE_REG) {
                    currentState = NumSourceStateEnum.PRE_SUCCESS;
                } else if (event == NumSourceEventEnum.CASH) {
                    currentState = NumSourceStateEnum.CASH;
                } else if (event == NumSourceEventEnum.REG) {
                    currentState = NumSourceStateEnum.REG_SUCCESS;
                } else if (event == NumSourceEventEnum.STOP) {
                    currentState = NumSourceStateEnum.STOP;
                } else {
                    throw new StateHandleException(currentState, event);
                }
                break;
            case PRE_SUCCESS:
                if (event == NumSourceEventEnum.PRE_REG_CANCEL) {
                    currentState = NumSourceStateEnum.LOCKING;
                } else if (event == NumSourceEventEnum.GET_NUM) {
                    currentState = NumSourceStateEnum.REG_SUCCESS;
                } else {
                    throw new StateHandleException(currentState, event);
                }
                break;
            case CASH:
                if (event == NumSourceEventEnum.CASH_SUCCESS) {
                    currentState = NumSourceStateEnum.REG_SUCCESS;
                } else if (event == NumSourceEventEnum.CASH_FAIL) {
                    currentState = NumSourceStateEnum.RELEASE;
                } else {
                    throw new StateHandleException(currentState, event);
                }
                break;
            default:
                throw new StateHandleException(currentState, event);
        }
    }
}
